package com.example.demo.service.impl;

import com.example.demo.entity.BuildingInfo;
import com.example.demo.entity.User;
import com.example.demo.exception.CmsException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.mapper.BuildingInfoMapper;
import com.example.demo.service.BuildingInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen huilong
 * @since 2022-10-04
 */
@Service
public class BuildingInfoServiceImpl extends ServiceImpl<BuildingInfoMapper, BuildingInfo> implements BuildingInfoService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public void insertBuildingData() {
        LinkedList<BuildingInfo> buildingInfos = new LinkedList<>();

        BuildingInfo buildingInfo = new BuildingInfo();
        buildingInfo.setBuildingName("5号楼");
        buildingInfo.setGender("2");
        buildingInfo.setRoomCountMan(30);
        buildingInfo.setBedCountMan(120);
        buildingInfo.setBedRemainderMan(80);
        buildingInfo.setRoomCountWoman(20);
        buildingInfo.setBedCountWoman(40);
        buildingInfo.setBedRemainderWoman(30);
        buildingInfos.add(buildingInfo);

        BuildingInfo buildingInfo1 = new BuildingInfo();
        buildingInfo1.setBuildingName("8号楼");
        buildingInfo1.setGender("0");
        buildingInfo1.setRoomCountMan(10);
        buildingInfo1.setBedCountMan(80);
        buildingInfo1.setBedRemainderMan(40);
        buildingInfo1.setRoomCountWoman(0);
        buildingInfo1.setBedCountWoman(0);
        buildingInfo1.setBedRemainderWoman(0);
        buildingInfos.add(buildingInfo1);

        BuildingInfo buildingInfo2 = new BuildingInfo();
        buildingInfo2.setBuildingName("9号楼");
        buildingInfo2.setGender("0");
        buildingInfo2.setRoomCountMan(12);
        buildingInfo2.setBedCountMan(70);
        buildingInfo2.setBedRemainderMan(60);
        buildingInfo2.setRoomCountWoman(0);
        buildingInfo2.setBedCountWoman(0);
        buildingInfo2.setBedRemainderWoman(0);
        buildingInfos.add(buildingInfo2);

        BuildingInfo buildingInfo3 = new BuildingInfo();
        buildingInfo3.setBuildingName("13号楼");
        buildingInfo3.setGender("1");
        buildingInfo3.setRoomCountMan(12);
        buildingInfo3.setBedCountMan(70);
        buildingInfo3.setBedRemainderMan(50);
        buildingInfo3.setRoomCountWoman(12);
        buildingInfo3.setBedCountWoman(60);
        buildingInfo3.setBedRemainderWoman(50);
        buildingInfos.add(buildingInfo3);

        BuildingInfo buildingInfo4 = new BuildingInfo();
        buildingInfo4.setBuildingName("14号楼");
        buildingInfo4.setGender("1");
        buildingInfo4.setRoomCountMan(12);
        buildingInfo4.setBedCountMan(70);
        buildingInfo4.setBedRemainderMan(50);
        buildingInfo4.setRoomCountWoman(20);
        buildingInfo4.setBedCountWoman(50);
        buildingInfo4.setBedRemainderWoman(30);
        buildingInfos.add(buildingInfo4);

        for (BuildingInfo info : buildingInfos) {
            baseMapper.insert(info);
        }
    }

    @Override
    public List<String> getBuildingList(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(request.getHeader("access_token"));
        List<BuildingInfo> buildingInfos = baseMapper.selectList(null);
        String gender = user.getGender();
        LinkedList<String> buildings = new LinkedList<>();
        for(BuildingInfo buildingInfo : buildingInfos) {
            if(gender.equals(buildingInfo.getGender()) || "2".equals(buildingInfo.getGender())) {
                buildings.add(buildingInfo.getBuildingName());
            }
        }
        return buildings;
    }

    @Override
    public HashMap<String, Integer> getBuildingById(String access_token, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(access_token);
        List<BuildingInfo> buildingInfos = baseMapper.selectList(null);
        String gender = user.getGender();
        HashMap<String, Integer> building_bed = new HashMap<>();
        for(BuildingInfo buildingInfo : buildingInfos) {
            if(gender.equals(buildingInfo.getGender()) || "2".equals(buildingInfo.getGender())) {
                if("0".equals(gender)) {
                    building_bed.put(buildingInfo.getBuildingName(), buildingInfo.getBedRemainderMan());
                } else {
                    building_bed.put(buildingInfo.getBuildingName(), buildingInfo.getBedRemainderWoman());
                }
            }
        }
        return building_bed;
    }

    @Override
    public void exportStuList() {

    }
}
